package com.prs.crm.report;

import java.awt.Color;
import java.awt.Font;
import java.text.DecimalFormat;
import java.text.NumberFormat;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartRenderingInfo;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryAxis3D;
import org.jfree.chart.axis.NumberAxis3D;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.imagemap.ImageMapUtilities;
import org.jfree.chart.imagemap.StandardToolTipTagFragmentGenerator;
import org.jfree.chart.imagemap.ToolTipTagFragmentGenerator;
import org.jfree.chart.imagemap.URLTagFragmentGenerator;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.labels.StandardCategoryToolTipGenerator;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer3D;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.chart.title.TextTitle;
import org.jfree.chart.urls.StandardCategoryURLGenerator;
import org.jfree.chart.urls.StandardPieURLGenerator;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.xy.XYDataset;
import org.jfree.ui.HorizontalAlignment;
import org.jfree.ui.TextAnchor;
import org.jfree.ui.VerticalAlignment;
import org.jfree.util.Rotation;

public class Chart3D extends ReportDateUtils {
	private final Font font = new Font("微软雅黑",Font.PLAIN,12);
	private final static Color gridLineColor = new Color(195,195,195);
	/**
	 * <h1>饼图</h1> 
	 * @param pieDataset 饼图数据源
	 * @param title 图形标题
	 * @param url 热点地址
	 * @return 返回jfreechart实例
	 * **/
	public JFreeChart getPieChart3D(DefaultPieDataset pieDataset, String title, String url){
		JFreeChart chart = ChartFactory.createPieChart3D(title, pieDataset, true, true, false);
		TextTitle ttitle = new TextTitle("提示:数据为0将不会显示!   ",font);
		ttitle.setVerticalAlignment(VerticalAlignment.BOTTOM);
		ttitle.setHorizontalAlignment(HorizontalAlignment.RIGHT);
		chart.addSubtitle(ttitle);
		chart.getTitle().setFont(font);
		chart.getLegend().setItemFont(font);
		PiePlot3D piePlot = (PiePlot3D)chart.getPlot();
		piePlot.setIgnoreNullValues(true);
		piePlot.setIgnoreZeroValues(true);
		piePlot.setBackgroundPaint(Color.white);
		piePlot.setNoDataMessage("No data to display");
		piePlot.setLabelFont(font);
		piePlot.setForegroundAlpha(0.6f);
		piePlot.setCircular(true);
		piePlot.setDirection(Rotation.CLOCKWISE);
		piePlot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0} ({1}={2})", NumberFormat.getNumberInstance(), new DecimalFormat("0.00%")));
		piePlot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator("{0} ({1}={2})", NumberFormat.getNumberInstance(), new DecimalFormat("0.00%")));
		piePlot.setOutlineVisible(false);
		piePlot.setURLGenerator(new StandardPieURLGenerator(url));
		return chart;
	}
	/**
	 * <h1>柱状图</h1> 
	 * @param pieDataset 饼图数据源
	 * @param title 图形标题
	 * @param valueTitle 左侧标题
	 * @param type 底部标题
	 * @return 返回jfreechart实例
	 * **/
	public JFreeChart getBarChart3D(DefaultCategoryDataset barDataset, String title, String valueTitle, String type){
		JFreeChart chart = ChartFactory.createBarChart3D(title, type,valueTitle,barDataset,PlotOrientation.VERTICAL,true,true,false);
		chart.getTitle().setFont(font);
		chart.getLegend().setItemFont(font);
		CategoryPlot plot = chart.getCategoryPlot();
		CategoryAxis axis = plot.getDomainAxis();
		axis.setTickLabelFont(font);
		axis.setLabelFont(font);
		plot.getRangeAxis().setTickLabelFont(font);
		plot.getRangeAxis().setLabelFont(font);
		return chart;
	}
	/**
	 * 带有热点的Jfreechart实例
	 * <h1>柱状图</h1> 
	 * @param barDataset 柱状图数据源
	 * @param title 图形标题
	 * @param valueTitle 左侧标题
	 * @param type 底部标题
	 * @param url 热点地址
	 * @return 返回jfreechart实例
	 * */
	public JFreeChart getHotBarChart3D(DefaultCategoryDataset barDataset, String title, String valueTitle, String type, String url){
		BarRenderer3D renderer = new BarRenderer3D();
		renderer.setBaseToolTipGenerator(new StandardCategoryToolTipGenerator());
		renderer.setBaseItemURLGenerator(new StandardCategoryURLGenerator(url));
		CategoryAxis3D categoryAxis = new CategoryAxis3D(type);
		ValueAxis valueAxis = new NumberAxis3D(valueTitle);
		CategoryPlot plot = new CategoryPlot(barDataset,categoryAxis, valueAxis, renderer);
		
		BarRenderer3D gir = (BarRenderer3D)plot.getRenderer();
		
		gir.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator("{2}",NumberFormat.getNumberInstance(), new DecimalFormat("0.00%")));
		
		gir.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE1, TextAnchor.BASELINE_CENTER)); 
		gir.setItemLabelAnchorOffset(10D);
		gir.setBaseItemLabelsVisible(true);
		gir.setBaseItemLabelPaint(Color.red);
		plot.setRenderer(gir);
		plot.setForegroundAlpha(0.8f);
		
		plot.getDomainAxis().setTickLabelFont(font);
		plot.getDomainAxis().setLabelFont(font);
		
		plot.getRangeAxis().setTickLabelFont(font);
		plot.getRangeAxis().setLabelFont(font);
		
		NumberAxis3D num = (NumberAxis3D)plot.getRangeAxis();
		num.setLowerMargin(0.1);
		num.setUpperMargin(0.1);
		num.setVisible(true);
		plot.setOrientation(PlotOrientation.VERTICAL);
		plot.setDomainGridlinesVisible(true);
		plot.setRangeGridlinesVisible(true);
		JFreeChart chart = new JFreeChart(title,JFreeChart.DEFAULT_TITLE_FONT, plot, true);
		chart.getTitle().setFont(font);
		chart.getLegend().setItemFont(font);
		chart.setBackgroundPaint(java.awt.Color.white);
		
		return chart;
	}
	/**
	 * <h1>曲线图</h1> 
	 * @param lineDataset 曲线图数据源
	 * @param title 图形标题
	 * @param valueTitle 左侧标题
	 * @param type 底部标题
	 * @param url 热点地址
	 * @return 返回jfreechart实例
	 * */
	public JFreeChart getLineChart(DefaultCategoryDataset lineDataset, String title, String valueTitle, String type, String url){
		JFreeChart chart = ChartFactory.createLineChart(title,type,valueTitle,lineDataset,PlotOrientation.VERTICAL,true,true,false);
		chart.getTitle().setFont(font);
		chart.getLegend().setItemFont(font);
		CategoryPlot plot = chart.getCategoryPlot();
		CategoryAxis axis = plot.getDomainAxis();
		axis.setTickLabelFont(font);
		axis.setLabelFont(font);
		plot.getRangeAxis().setTickLabelFont(font);
		plot.getRangeAxis().setLabelFont(font);
		plot.setDomainGridlinesVisible(true);
		plot.setDomainGridlinePaint(gridLineColor);
		plot.setRangeGridlinesVisible(true);
		plot.setRangeGridlinePaint(gridLineColor);
		plot.setBackgroundPaint(java.awt.Color.white);
		LineAndShapeRenderer renderer = (LineAndShapeRenderer)plot.getRenderer();
		renderer.setBaseShapesVisible(true);
		renderer.setBaseShapesFilled(true);
		renderer.setBaseItemURLGenerator(new org.jfree.chart.urls.StandardCategoryURLGenerator(url));
		return chart;
	}
	/**
	 * <h1>曲线图</h1> 
	 * @param lineDataset 曲线图数据源
	 * @param title 图形标题
	 * @param valueTitle 左侧标题
	 * @param type 底部标题
	 * @param url 热点地址
	 * @return 返回jfreechart实例
	 * */
	public JFreeChart getXYLineChart(XYDataset lineDataset, String title, String valueTitle, String type, String url){
		JFreeChart chart = ChartFactory.createXYLineChart(title,type,valueTitle,lineDataset,PlotOrientation.VERTICAL,true,true,false);
		chart.getTitle().setFont(font);
		chart.getLegend().setItemFont(font);
		CategoryPlot plot = chart.getCategoryPlot();
		CategoryAxis axis = plot.getDomainAxis();
		axis.setTickLabelFont(font);
		axis.setLabelFont(font);
		plot.getRangeAxis().setTickLabelFont(font);
		plot.getRangeAxis().setLabelFont(font);
		plot.setDomainGridlinesVisible(true);
		plot.setRangeGridlinesVisible(true);
		LineAndShapeRenderer renderer = (LineAndShapeRenderer)plot.getRenderer();
		renderer.setBaseShapesVisible(true);
		renderer.setBaseShapesFilled(true);
		renderer.setBaseItemURLGenerator(new org.jfree.chart.urls.StandardCategoryURLGenerator(url));
		return chart;
	}
	/**
	 * 获取热点信息：
	 * @return html.map信息
	 * **/
	public String getHotImageMaps(String mapName, ChartRenderingInfo info){
		ToolTipTagFragmentGenerator toolTipTagFragmentGenerator = new StandardToolTipTagFragmentGenerator();
		URLTagFragmentGenerator urlTagFragmentGenerator = new StandardURLTagFragmentGenerator();
		return ImageMapUtilities.getImageMap(mapName, info, toolTipTagFragmentGenerator, urlTagFragmentGenerator);
		
	}
	
	/**
	 * DefaultCategoryDataset Conversion DefaultPieDataset
	 * @param DefaultCategoryDataset dataset
	 * @return DefaultPieDataset pieDataset
	 * */
	public DefaultPieDataset getDefaultPieDataset(DefaultCategoryDataset dataset){
		DefaultPieDataset pieDataset = new DefaultPieDataset();
		int rowCount = dataset.getRowCount();
		int columnCount = dataset.getColumnCount();
		for(int row = 0; row < rowCount; row++){
			for(int column = 0; column < columnCount; column++){
				pieDataset.setValue(dataset.getColumnKey(column),dataset.getValue(row,column));
			}
		}
		return pieDataset;
	}
	/**
	 * 带单位参数类型转换
	 * **/
	public DefaultPieDataset getDefaultPieDataset(DefaultCategoryDataset dataset, String unit){
		DefaultPieDataset pieDataset = new DefaultPieDataset();
		int rowCount = dataset.getRowCount();
		int columnCount = dataset.getColumnCount();
		for(int row = 0; row < rowCount; row++){
			for(int column = 0; column < columnCount; column++){
				pieDataset.setValue((column + 1) + unit,dataset.getValue(row,column));
			}
		}
		return pieDataset;
	}
}

